<?php

class DefaultController extends Controller
{
	public function filters()
    {
        return array(
            'accessControl',
        );
    }
	
	public function accessRules()
    {
        return array(
			array('allow',
				'actions'=>array('login'),
				'users'=>array('?'),
			),
			array('allow',
				'actions'=>array('index', 'logout', 'config', 'addConfig', 'updateConfig', 'deleteConfig', 'writeConfig'),
				'users'=>array('@'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
    }
	
	public function actionIndex()
	{
		$model = Events::model()->findAllByAttributes(array(),array(
				        'limit'=>10,
				        'order'=>'posteddate DESC',
				        'condition'=>'status="A" AND enddate >= NOW()'	));
		$this->render('index',array('model'=>$model));
	}
	
	public function actionLogin()
	{
		$this->layout = '//layouts/defaultLogin';
		$model = new LoginForm;
		
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			if($model->validate() && $model->login()) {
				$this->redirect(array('/Dashboard/default/index'));
			}
		}
		
		$this->render('login', array(
			'model'=>$model
		));
	}
	
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	public function actionConfig()
	{
		$model = new Configs;
		$dataProvider=new CActiveDataProvider('Configs',
			array(
				'sort'=>array(
					'defaultOrder'=>'id ASC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		
		$this->render('config', array(
			'dataProvider'=>$dataProvider,
			'model'=>$model
		));
	}
	
	public function actionAddConfig()
	{
		$model = new Configs;
		if (isset($_POST['Configs'])) {
			$model->attributes = $_POST['Configs'];
			$model->posteddate = $model->updatedate = date('Y-m-d H:i:s');
			if ($model->save()) {
				$this->redirect(array('/Dashboard/default/config'));
			}
		}
	}
	
	public function actionUpdateConfig($id)
	{
		$model = Configs::model()->findByPk($id);
		
		if (isset($_POST['Configs'])) {
			$model->attributes = $_POST['Configs'];
			$model->updatedate = date('Y-m-d H:i:s');
			if ($model->save()) {
				$this->redirect(array('/Dashboard/default/config'));
			}
		}
		
		$this->render('updateConfig', array(
			'model'=>$model
		));
	}
	
	public function actionDeleteConfig($id)
	{
		$model = Configs::model()->findByPk($id)->delete();

		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function actionWriteConfig()
	{
		$model = Configs::model()->findAll();
		
		$fp = fopen(CONFIG_MAIN_FILE, "w");
		fwrite($fp, "<?php".PHP_EOL);
        fclose($fp);
        foreach ($model as $x => $data) {
            if ($x >= 0) {
                file_put_contents(CONFIG_MAIN_FILE, "define('$data->key', '".str_replace("'", "\'", $data->value)."');".PHP_EOL, FILE_APPEND);
            }
            else {
                file_put_contents(CONFIG_MAIN_FILE, "define('$data->key', '".str_replace("'", "\'", $data->value)."');".PHP_EOL);
            }
        }
        /*
		foreach ($model as $data) {
			fwrite($fp, mb_convert_encoding ("define('$data->key', '".str_replace("'", "\'", $data->value)."');".PHP_EOL, 'UTF-8'));
		}
		fclose($fp);
		*/
		$this->redirect(array('/Dashboard/default/config'));
	}
}